/*
NO OTHER WARRANTIES.  PRINCIPE SOFTWARE DOES NOT WARRANT
THAT THE SOFTWARE IS ERROR FREE.
PRINCIPE SOFTWARE DISCLAIMS ALL OTHER WARRANTIES WITH RESPECT
TO THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES
OR LIMITATIONS ON HOW LONG AN IMPLIED WARRANTY MAY LAST, OR THE
EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES,
SO THE ABOVE LIMITATIONS OR EXCLUSIONS MAY NOT APPLY TO YOU.
THIS WARRANTY GIVES YOU SPECIFIC LEGAL RIGHTS AND YOU MAY
ALSO HAVE OTHER RIGHTS WHICH VARY FROM JURISDICTION TO JURISDICTION.

(c) Copyright 2013 Principe Software
All Rights Reserved

*/
#ifndef TOKNODE_H
#define TOKNODE_H

#include <Windows.h>
#include <tchar.h>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include "tok.h"
#include "cantreadfile.h"
#include "cantwritefile.h"

using namespace std;

//////////////////////////////////////////////////////////////////////
// TokNode: A single token
//////////////////////////////////////////////////////////////////////
struct TokNode
{
public: // Construction
    TokNode()
    {
        Init();
    }

    TokNode(const TokNode& rhs)
    {
        Init();
        Copy(rhs);
    }

    ~TokNode()
    {
        delete data;
        delete astring;
    }

public: // Attributes
   TokType type; // The type
   TokData *data; // The token data
   wstring *astring; // If string data; store here

private: // Methods
    void Init()
    {
        type = TT_INT;
		data = NULL;
        astring = NULL;
    }

    void Copy(const TokNode& rhs)
    {
        if(this == &rhs)
            return;

        delete data;
        delete astring;

        type = rhs.type;

        if(type == TT_STRING)
        {
            astring = new wstring();
            *astring = *rhs.astring;
        }
        else
        {
			data = new TokData;
            memcpy((void *)data, (void *)rhs.data, sizeof(*data));
        }
    }

public: // methods
	void Reset()
	{
		delete data;
		delete astring;

		data = NULL;
		astring = NULL;
	}

public: // Methods
    void Read(fstream& in); // Reads a token in
    void Write(fstream& out); // Writes a token out

public: // Operators
    TokNode& operator = (const TokNode& rhs)
    {
        Copy(rhs);
        return *this;
    }

#ifdef _DEBUG
public: // Debug methods
	void Dump();
#endif
};
#endif // TOKNODE_H
